﻿using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Text;

namespace MinConsole.Gaf.Utils
{

    /// <summary>
    // Dumper - provides methods API for returning nice looking ; ) dump strings of various types of types during run-time 
    /// </summary>
    public class Dumper
    {

        /// <summary>
        /// This method outputs on values of the passed dataset objects for easier debugging
        /// </summary>
        /// <param name="global.msg"> the string passed containng usual info</param>
        /// <param name="ds"> the dataset , which content one should debug </param>
        /// call it this way  //DataSet (  System.Reflection.MethodInfo.GetCurrentMethod().Name() ,  , ref ds );         
        /// <returns> the string containing the nice looking dump of the object </returns>
        public static string DumpDs(string msg, ref System.Data.DataSet ds)
        {

            StringBuilder objStringBuilder = new StringBuilder();
            if (ds == null)
            {
                objStringBuilder.AppendLine("Null dataset passed ");
                return objStringBuilder.ToString();
            }

            objStringBuilder.AppendLine("===================================================" + msg + " START ");
            if (ds != null)
            {
                if (ds.Tables == null)
                {
                    objStringBuilder.AppendLine("ds.Tables == null ");
                    return objStringBuilder.ToString();
                }

                objStringBuilder.AppendLine(msg);
                foreach (System.Data.DataTable dt in ds.Tables)
                {
                    if (dt == null)
                    {
                        objStringBuilder.AppendLine("ds.Tables == null ");
                        continue;
                    }

                    objStringBuilder.AppendLine("================= My TableName is  " +
                    dt.TableName + " ========================= START");
                    int colNumberInRow = 0;

                    foreach (System.Data.DataColumn dc in dt.Columns)
                    {
                        if (dc == null)
                        {
                            objStringBuilder.AppendLine("ds.Tables == null ");
                            continue;
                        }

                        objStringBuilder.Append(" | ");
                        objStringBuilder.Append(" |" + colNumberInRow + "| ");
                        objStringBuilder.Append(dc.ColumnName + " | ");
                        colNumberInRow++;
                    } //eof foreach (DataColumn dc in dt.Columns)
                    int rowNum = 0;
                    foreach (System.Data.DataRow dr in dt.Rows)
                    {
                        objStringBuilder.Append("\n row " + rowNum + " --- ");
                        int colNumber = 0;
                        foreach (System.Data.DataColumn dc in dt.Columns)
                        {
                            objStringBuilder.Append(" |" + colNumber + "| ");
                            objStringBuilder.Append(dr[dc].ToString() + " ");
                            colNumber++;
                        } //eof foreach (DataColumn dc in dt.Columns)
                        rowNum++;
                    }	//eof foreach (DataRow dr in dt.Rows)
                    objStringBuilder.Append(" \n");
                    objStringBuilder.AppendLine("================= Table " +
    dt.TableName + " ========================= END");
                    objStringBuilder.AppendLine("===================================================" + msg + " END ");
                }	//eof foreach (DataTable dt in ds.Tables)
            } //eof if ds !=null 
            else
            {
                objStringBuilder.AppendLine("NULL DataSet object passed for debugging !!!");
            }
            return objStringBuilder.ToString();
        } //eof method 

        /// <summary>
        /// This method outputs on values of the passed dataset objects for easier debugging
        /// </summary>
        /// <param name="global.msg"> the string passed containng usual info</param>
        /// <param name="ds"> the dataset , which content one should debug </param>
        /// call it this way  //DataSet (  System.Reflection.MethodInfo.GetCurrentMethod().Name() ,  , ref ds );         
        /// <returns> the string containing the nice looking dump of the object </returns>
        public static string DumpDt(string msg, ref System.Data.DataTable dt)
        {

            StringBuilder objStringBuilder = new StringBuilder();

            objStringBuilder.AppendLine("===================================================" + msg + " START ");
            if (dt == null)
            {
                objStringBuilder.AppendLine("ds.Tables == null ");

            }

            objStringBuilder.AppendLine("================= My TableName is  " +
            dt.TableName + " ========================= START");
            int colNumberInRow = 0;

            foreach (System.Data.DataColumn dc in dt.Columns)
            {
                if (dc == null)
                {
                    objStringBuilder.AppendLine("ds.Tables == null ");
                    continue;
                }

                objStringBuilder.Append(" | ");
                objStringBuilder.Append(" |" + colNumberInRow + "| ");
                objStringBuilder.Append(dc.ColumnName + " | ");
                colNumberInRow++;
            } //eof foreach (DataColumn dc in dt.Columns)
            int rowNum = 0;
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                objStringBuilder.Append("\n row " + rowNum + " --- ");
                int colNumber = 0;
                foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    objStringBuilder.Append(" |" + colNumber + "| ");
                    objStringBuilder.Append(dr[dc].ToString() + " ");
                    colNumber++;
                } //eof foreach (DataColumn dc in dt.Columns)
                rowNum++;
            }	//eof foreach (DataRow dr in dt.Rows)
            objStringBuilder.Append(" \n");
            objStringBuilder.AppendLine("================= Table " +
dt.TableName + " ========================= END");
            objStringBuilder.AppendLine("===================================================" + msg + " END ");

            return objStringBuilder.ToString();
        } //eof method 

        /// <summary>
        /// Dumps the passed DataSet obj for debugging as list of html tables
        /// </summary>
        /// <param name="msg"> the msg attached </param>
        /// <param name="ds"> the DataSet object passed for Dumping </param>
        /// <returns> the nice looking dump of the DataSet obj in html format</returns>
        public static string DumpHtmlDs(string msg, ref System.Data.DataSet ds)
        {
            StringBuilder objStringBuilder = new StringBuilder();
            objStringBuilder.AppendLine("<html><body>");

            if (ds == null)
            {
                objStringBuilder.AppendLine("Null dataset passed ");
                objStringBuilder.AppendLine("</html></body>");
                WriteIf(objStringBuilder.ToString());
                return objStringBuilder.ToString();
            }

            objStringBuilder.AppendLine("<p>" + msg + " START </p>");
            if (ds != null)
            {
                if (ds.Tables == null)
                {
                    objStringBuilder.AppendLine("ds.Tables == null ");
                    return objStringBuilder.ToString();
                }


                foreach (System.Data.DataTable dt in ds.Tables)
                {

                    if (dt == null)
                    {
                        objStringBuilder.AppendLine("ds.Tables == null ");
                        continue;
                    }
                    objStringBuilder.AppendLine("<table>");

                    //objStringBuilder.AppendLine("================= My TableName is  " +
                    //dt.TableName + " ========================= START");
                    int colNumberInRow = 0;
                    objStringBuilder.Append("<tr><th>row number</th>");
                    foreach (System.Data.DataColumn dc in dt.Columns)
                    {
                        if (dc == null)
                        {
                            objStringBuilder.AppendLine("DataColumn is null ");
                            continue;
                        }


                        objStringBuilder.Append(" <th> |" + colNumberInRow.ToString() + " | ");
                        objStringBuilder.Append(dc.ColumnName.ToString() + " </th> ");
                        colNumberInRow++;
                    } //eof foreach (DataColumn dc in dt.Columns)
                    objStringBuilder.Append("</tr>");

                    int rowNum = 0;
                    foreach (System.Data.DataRow dr in dt.Rows)
                    {
                        objStringBuilder.Append("<tr><td> row - | " + rowNum.ToString() + " | </td>");
                        int colNumber = 0;
                        foreach (System.Data.DataColumn dc in dt.Columns)
                        {
                            objStringBuilder.Append(" <td> |" + colNumber + "|");
                            objStringBuilder.Append(dr[dc].ToString() + "  </td>");
                            colNumber++;
                        } //eof foreach (DataColumn dc in dt.Columns)
                        rowNum++;
                        objStringBuilder.AppendLine(" </tr>");
                    }	//eof foreach (DataRow dr in dt.Rows)

                    objStringBuilder.AppendLine("</table>");
                    objStringBuilder.AppendLine("<p>" + msg + " END </p>");
                }	//eof foreach (DataTable dt in ds.Tables)

            } //eof if ds !=null 
            else
            {

                objStringBuilder.AppendLine("NULL DataSet object passed for debugging !!!");
            }
            return objStringBuilder.ToString();

        } //eof method 






        public static string DebugDataTable(string msg, ref DataTable dt)
        {
            StringBuilder objStringBuilder = new StringBuilder();
            if (dt == null)
            {
                objStringBuilder.AppendLine("DebugDataTable I have been passed a null DataTable ");
                return objStringBuilder.ToString(); ;
            }
            objStringBuilder.AppendLine("================= My TableName is  " +
            dt.TableName + " ========================= START");
            int colNumberInRow = 0;
            foreach (System.Data.DataColumn dc in dt.Columns)
            {
                objStringBuilder.Append(" | ");
                objStringBuilder.Append(" |" + colNumberInRow + "| ");
                objStringBuilder.Append(dc.ColumnName + " | ");
                colNumberInRow++;
            } //eof foreach (DataColumn dc in dt.Columns)
            int rowNum = 0;
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                objStringBuilder.Append("\n row " + rowNum + " --- ");
                int colNumber = 0;
                foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    objStringBuilder.Append(" |" + colNumber + "| ");
                    objStringBuilder.Append(dr[dc].ToString() + " ");
                    colNumber++;
                } //eof foreach (DataColumn dc in dt.Columns)
                rowNum++;
            }	//eof foreach (DataRow dr in dt.Rows)
            objStringBuilder.Append(" \n");
            objStringBuilder.AppendLine("================= Table " +
            dt.TableName + " ========================= END");
            objStringBuilder.AppendLine("===================================================" + msg + " END ");
            return objStringBuilder.ToString();
        } //eof method

        public static string DebugHtmlDataTable(string msg, ref DataTable dt)
        {
            StringBuilder objStringBuilder = new StringBuilder();
            if (dt == null)
            {
                objStringBuilder.AppendLine("DebugDataTable I have been passed a null DataTable ");
                return objStringBuilder.ToString(); ;
            }
            objStringBuilder.AppendLine("<p> My TableName is  " +
            dt.TableName + "  START </p>");
            int colNumberInRow = 0;
            foreach (System.Data.DataColumn dc in dt.Columns)
            {
                objStringBuilder.AppendLine(" <td>");
                objStringBuilder.AppendLine(" |" + colNumberInRow.ToString() + "| ");
                objStringBuilder.AppendLine(dc.ColumnName.ToString() + " </td> ");
                colNumberInRow++;
            } //eof foreach (DataColumn dc in dt.Columns)
            int rowNum = 0;
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                objStringBuilder.AppendLine("<tr>" + rowNum + " --- ");
                int colNumber = 0;
                foreach (System.Data.DataColumn dc in dt.Columns)
                {
                    objStringBuilder.AppendLine(" <td> |" + colNumber.ToString());
                    objStringBuilder.AppendLine(dr[dc].ToString() + "</td> ");
                    colNumber++;
                } //eof foreach (DataColumn dc in dt.Columns)
                rowNum++;
            }	//eof foreach (DataRow dr in dt.Rows)

            objStringBuilder.AppendLine("</table>");
            objStringBuilder.AppendLine("<p>Table " +
            dt.TableName + " END </p>");
            objStringBuilder.AppendLine("<p>" + msg + " END </p>");

            return objStringBuilder.ToString();
        } //eof method


        /// <summary>
        /// This is the main debugging method, it outputs the debugging messages echoed in the code 
        /// according to the output , which is set in the web.config file, read the comments for each case 
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteIf(string msg)
        { 

        } //eof method WriteIf





        public static void IsNull(object o)
        {
            if (o == null)
            {
                WriteIf(o.ToString() + " IS null ");
                return;
            }
            else
            {
                WriteIf(o.ToString() + " IS NOT null");
            }
        } //eof IsNull



        /// <summary>
        /// This method outputs on values of the passed dataset objects for easier debugging
        /// </summary>
        /// <param name="global.msg"> the string passed containng usual info</param>
        /// <param name="ds"> the dataset , which content one should debug </param>
        /// call it this way  //DataSet (  System.Reflection.MethodInfo.GetCurrentMethod().Name() ,  , ref ds );         
        /// <returns> the string containing the nice looking dump of the object </returns>
        
        
        
        
        public static string DumpDsQoteColumns(
            ref System.Data.DataSet ds, int? tokenDelimiterAsciiNumber, int? RowEndAsciiNumber )
        {
            string strTokenDelimiter = Convert.ToString((char)(tokenDelimiterAsciiNumber)) ?? "\"" ;
            string strRowEnd = Convert.ToString((char)(RowEndAsciiNumber)) ?? "\n" ;
            string strQuote = "\""; 

            StringBuilder objStringBuilder = new StringBuilder();




            if (ds == null)
            {
                objStringBuilder.AppendLine("No result data in result set ");
                return objStringBuilder.ToString();
            }

            
            if (ds != null)
            {
                if (ds.Tables == null)
                {
                    objStringBuilder.AppendLine("ds.Tables == null. No result data in result set ");
                    return objStringBuilder.ToString();
                }

                
                foreach (System.Data.DataTable dt in ds.Tables)
                {
                    if (dt == null)
                    {
                        objStringBuilder.AppendLine("ds.Tables == null. No result data in result set ");
                        continue;
                    }

                    int colNumberInRow = 0;

                    #region ColumnTitles 
                    //foreach (System.Data.DataColumn dc in dt.Columns)
                    //{
                    //    if (dc == null)
                    //    {
                    //        objStringBuilder.AppendLine("dc.Columns== null ");
                    //        continue;
                    //    }

                    //    objStringBuilder.Append(" | ");
                    //    objStringBuilder.Append(" |" + colNumberInRow + "| ");
                    //    objStringBuilder.Append(dc.ColumnName + " | ");
                    //    colNumberInRow++;
                    //} //eof foreach (DataColumn dc in dt.Columns)
                    #endregion ColumnTitles 

                    int rowNum = 0;

                    foreach (System.Data.DataRow dr in dt.Rows)
                    {
                        int colNumber = 0;
                        foreach (System.Data.DataColumn dc in dt.Columns)
                        {
                            objStringBuilder.Append(strQuote + dr[dc].ToString() + strQuote + strTokenDelimiter);
                            colNumber++;
                        } //eof foreach (DataColumn dc in dt.Columns)
                        objStringBuilder.Remove(objStringBuilder.Length - 1 , 1);   //remove the last " , " 
                        objStringBuilder.Append(strRowEnd);

                        rowNum++;
                    }	//eof foreach (DataRow dr in dt.Rows)

                }	//eof foreach (DataTable dt in ds.Tables)
            } //eof if ds !=null 
            else
            {
                objStringBuilder.AppendLine("NULL DataSet object passed for debugging !!!");
            }
            return objStringBuilder.ToString();
        } //eof method 



    } //eof class 
} //eof namespace 

